﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Bird.OAWeb.Service.IService;
using Bird.OAWeb.Domain.DTO;
using Bird.OAWeb.Ropesitory;
using Bird.OAWeb.Domain.Table;
using Org.BouncyCastle.Bcpg;

namespace Bird.OAWeb.Service.Service
{
    public  class RoleService:IRoleService
    {
        /// <summary>
        /// 仓储接口注入
        /// </summary>
        IBaseRopesitory<Role> RoleRepository;

        public RoleService(IBaseRopesitory<Role> roleRepository)
        {
            RoleRepository = roleRepository;
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int RoleAdd(Role model)
        {
            try
            {
                return RoleRepository.Inset(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 角色反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Role RoleBind(int id)
        {
           return  RoleRepository.GetById(id);
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool RoleDelete(int id)
        {
            return RoleRepository.DeleteById(id);
        }
        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageList<RoleDto> RoleShow(int pageIndex = 1, int pageSize = 2)
        {
            var list = from s in RoleRepository.GetList()
                       select new RoleDto
                       {
                           Status= s.Status,
                           RoleId=s.RoleId,
                           CreatedTime=s.CreatedTime,
                           RoleDescription=s.RoleDescription,
                           RoleName=s.RoleName,
                           UpdatedTime=s.UpdatedTime,
                       };
            var total = list.Count();
            var data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return new PageList<RoleDto>
            {
                total = total,
                data = data.ToList()
            };
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool RoleUpdate(Role model)
        {
            return RoleRepository.Update(model);
        }
    }
}
